# -*- coding: utf-8 -*-

class Service(object):
    def __init__(self, sid = 0):
        self.sid = sid
        self.__command_map = {}

    def handle(self, msg):
        cid = msg['cid']
        if cid not in self.__command_map:
            raise Exception('bad command %s' % cid)
        f = self.__command_map[cid]
        return f(msg)

    def register(self,cid ,function):
        self.__command_map[cid] = function

    def registers(self, commandDict):
        self.__command_map = {}
        for cid in commandDict:
            self.register(cid, commandDict[cid])
        return 0